#!/bin/sh
case "$1" in
  start)
    mkdir -p /tmp/gpio/ledcontrol
    mkdir -p /tmp/conman/status
    # Allow apps to notify activity
    chgrp video /tmp/gpio
    chmod 775 /tmp/gpio
    # Allow apps to specify when they are in waitpower mode
    chgrp video /tmp/gpio/ledcontrol
    chmod 775 /tmp/gpio/ledcontrol
    mkdir -p /tmp/gpio/ledcontrol
    setsid babysit 60 linkmonitor /tmp/gpio/ledcontrol/ip.changed 2>&1 |
      logos linkmonitor &
    setsid babysit 60 ipmonitor /tmp/gpio/ledcontrol/ip.changed 2>&1 |
      logos ipmonitor &
    setsid babysit 60 ledmonitor 2>&1 | logos ledmonitor &
    ;;
  stop)

    for pgid in $(ps -Clinkmonitor -o pgid= | sort -u); do
      pkillwait -g $pgid
    done
    for pgid in $(ps -Cipmonitor -o pgid= | sort -u); do
      pkillwait -g $pgid
    done
    for pgid in $(ps -Cledmonitor -o pgid= | sort -u); do
      pkillwait -g $pgid
    done
    ;;
  restart)
    $0 stop; $0 start
      ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
esac
